package com.czy.daishusys.ui;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.czy.daishusys.R;
import com.czy.daishusys.db.UserHelper;
import com.czy.daishusys.utils.GetDeviceId;
import com.czy.daishusys.utils.MyLog;
import butterknife.BindView;

/**
 * @author: Raven
 * @date: 2017/11/27 15:16
 * @email: xiexiaomeng_001@163.com
 * @description: 通用网页页面
 */
public class WebActivity extends BaseActivity {

    public static final String TITLE_PARAM = "title_param";
    public static final String URL_PARAM = "url_param";
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.myProgressBar)
    ProgressBar myProgressBar;
    @BindView(R.id.webview)
    WebView webview;

    private String postData;

    /**
     * 跳转的Web页面
     *
     * @param context 源页面上下文
     * @param title   web页面标题
     * @param url     web页面链接
     */
    public static void navTo(Context context, String title, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(TITLE_PARAM, title);
        intent.putExtra(URL_PARAM, url);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_web;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        String title = getIntent().getStringExtra(TITLE_PARAM);
        String url = getIntent().getStringExtra(URL_PARAM);
        getToolbarTitle().setText(title == null ? "" : title);
        webview.setWebViewClient(new MyWebViewClient());
        String session = UserHelper.getInstance().getSessionInfoModel(this).getSession();
        String pid = new GetDeviceId(this).getCombinedId();
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setUseWideViewPort(false);  //将图片调整到适合webview的大小
        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        url = url+"?session=" + session + "&pid="+pid;
        MyLog.d(TAG,"url = " + url);
        webview.loadUrl(url);

        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    if (myProgressBar != null) {
                        myProgressBar.setVisibility(View.GONE);
                    }
                    if (myProgressBar != null) {
                        myProgressBar.setVisibility(View.GONE);
                    }
                } else {
                    myProgressBar.setVisibility(View.VISIBLE);
                    myProgressBar.setProgress(newProgress);

                }
            }

        });
    }

    @Override
    protected void updateViews() {

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    }

}
